package search;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Vector;

import graph.IdManager;
import graph.PropertyManager;
import graph.PropertyManager.PropertyManagerIterator;
import graph.PropertyManager.PropertyRecord;
import util.Common;

public class Lister {
	static class PropertyRecordComp implements Comparator<PropertyRecord> {

		@Override
		public int compare(PropertyRecord arg0, PropertyRecord arg1) {
			return (Integer) arg1.m_data.getData()
					- (Integer) arg0.m_data.getData();
		}

	}

	public void list(File listFile) {
		try {
			PrintWriter writer = new PrintWriter(new FileWriter(listFile));
			IdManager idManager = Common.getIdManager("tweets");
			PropertyManager manager = Common.getPropertyManager("favorite");
			TwitterSearcher searcher = Common.getSearcher();
			PropertyManagerIterator it = new PropertyManagerIterator(manager);
			PriorityQueue<PropertyRecord> heap = new PriorityQueue<PropertyRecord>(
					10, new PropertyRecordComp());
			while (it.hasNext()) {
				PropertyRecord record = it.next();
				heap.add(record);
				System.out.printf("%d %d\n", record.m_id, record.m_data.getData());
			}
			while (!heap.isEmpty()) {
				PropertyRecord record = heap.poll();
				String textId = idManager.getText(record.m_id);
				TwitterRecord tRecord = searcher.getRecord(textId);
				tRecord.m_favorite = (Integer) record.m_data.getData();
				writer.print(tRecord);
			}
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	static public void main(String[] args) {
		Lister lister = new Lister();
		lister.list(new File("../data/favorite.dat"));
	}
}
